﻿
#include "MapViewController.h"
#include "AmapView.h"

MapViewController::MapViewController() :m_pMarker(NULL)
{
}

MapViewController::~MapViewController()
{

}

void MapViewController::viewDidLoad()
{
    // Do any additional setup after loading the view from its nib.
	
	this->setNavigationBarItem(CANavigationBarItem::create(UTF8("地图")));

	const DSize& viewSize = this->getView()->getFrame().size;
		
	amapView = AmapView::createWithFrame(DRect(0, 0, viewSize.width, viewSize.height * 0.5));
	amapView->setMapCenter(33.20, 120.66);
	amapView->setMapZoomLevel(5);
	amapView->setAmapViewDelegate(this);
    
    //AmapMarker* mark1 = new AmapMarker("r/btn_right_blue.png", true, UTF8("标题1"), UTF8("内容1"), 33, 120.52);
    //amapView->addMarker(mark1);
    //mark1->release();
    //
    //AmapMarker* mark2 = new AmapMarker("r/btn_right_blue.png", true, UTF8("标题2"), UTF8("内容2"), 33, 122.52);
    //amapView->addMarker(mark2);
    //mark2->release();
    //
    //AmapMarker* mark3 = new AmapMarker("", true, UTF8("标题2"), UTF8("内容2"), 30 + 3, 122.9);
    //amapView->addMarker(mark3);
    //mark3->release();
    
	//amapView->locationWhenMapShow(true);
	this->getView()->addSubview(amapView);

	m_pLoadedLb = CALabel::createWithFrame(DRect(50, 100 + viewSize.height * 0.5, 500, 100));
	m_pLoadedLb->setFontSize(32);
	m_pLoadedLb->setColor(CAColor_black);
	m_pLoadedLb->setTextAlignment(CATextAlignmentLeft);
	m_pLoadedLb->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	this->getView()->addSubview(m_pLoadedLb);

	m_pLatLngLb = CALabel::createWithFrame(DRect(50, 100 + viewSize.height * 0.5 + 100, viewSize.width - 50, 100));
	m_pLatLngLb->setFontSize(32);
	m_pLatLngLb->setColor(CAColor_black);
	m_pLatLngLb->setTextAlignment(CATextAlignmentLeft);
	m_pLatLngLb->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	this->getView()->addSubview(m_pLatLngLb);

	CAButton* addbt = CAButton::createWithFrame(DRect(50, 100 + viewSize.height * 0.5 + 100 + 100, 200, 90), CAButtonTypeRoundedRect);
	addbt->setTitleFontSize(30);
	addbt->setTitleForState(CAControlStateAll, UTF8("添加marker"));
	addbt->setTitleColorForState(CAControlStateAll, CAColor_black);
	addbt->addTarget(this, CAControl_selector(MapViewController::addMarkerBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(addbt);

	CAButton* delbt = CAButton::createWithFrame(DRect(50 + 250, 100 + viewSize.height * 0.5 + 100 + 100, 200, 90), CAButtonTypeRoundedRect);
	delbt->setTitleFontSize(30);
	delbt->setTitleForState(CAControlStateAll, UTF8("删除marker"));
	delbt->setTitleColorForState(CAControlStateAll, CAColor_black);
	delbt->addTarget(this, CAControl_selector(MapViewController::delMarkerBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(delbt);

	CAButton* clearbt = CAButton::createWithFrame(DRect(50 + 250 + 250, 100 + viewSize.height * 0.5 + 100 + 100, 200, 90), CAButtonTypeRoundedRect);
	clearbt->setTitleFontSize(30);
	clearbt->setTitleForState(CAControlStateAll, UTF8("清空marker"));
	clearbt->setTitleColorForState(CAControlStateAll, CAColor_black);
	clearbt->addTarget(this, CAControl_selector(MapViewController::clearMarkerBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(clearbt);
}

void MapViewController::viewDidUnload()
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

int i = 1;
void MapViewController::addMarkerBtClicked(CAControl* ctrl, DPoint point)
{
	AmapMarker* mark1 = new AmapMarker("r/btn_right_blue.png", true, UTF8("标题1"), UTF8("内容1"), 30 + i, 121.52);
	amapView->addMarker(mark1);

	mark1->release();

	i++;
}

void MapViewController::delMarkerBtClicked(CAControl* ctrl, DPoint point)
{
	if (amapView->getAllMarkers().size() > 0)
	{
		amapView->deleteMarker(amapView->getAllMarkers().begin()->second);
	}
}

void MapViewController::clearMarkerBtClicked(CAControl* ctrl, DPoint point)
{
	amapView->clearMarkers();
}

void MapViewController::onAmapLoaded()
{
	CCLog("void MapViewController::onAmapLoaded()--------------------------------------");
	CAScheduler::schedule(schedule_selector(MapViewController::amaploaded), this, 0);
}

void MapViewController::onAmapClicked(double lat, double lng)
{
	CCLog("void MapViewController::onAmapClicked()--------------------------------------");

	m_DLat = lat;
	m_DLng = lng;
	CAScheduler::schedule(schedule_selector(MapViewController::amapclicked), this, 0);
}

void MapViewController::onAmapMarkerDragStart(AmapMarker* marker, double lat, double lng)
{
	CCLog("void MapViewController::onAmapMarkerDragStart()--------------------------------------");

	m_DLat = lat;
	m_DLng = lng;
	m_pMarker = marker;

	CAScheduler::schedule(schedule_selector(MapViewController::amapmarkerdragstart), this, 0);
}

void MapViewController::onAmapMarkerDrag(AmapMarker* marker, double lat, double lng)
{
	CCLog("void MapViewController::onAmapMarkerDrag()--------------------------------------");

	m_DLat = lat;
	m_DLng = lng;
	m_pMarker = marker;

	CAScheduler::schedule(schedule_selector(MapViewController::amapmarkerdrag), this, 0);
}

void MapViewController::onAmapMarkerDragEnd(AmapMarker* marker, double lat, double lng)
{
	CCLog("void MapViewController::onAmapMarkerDragEnd()--------------------------------------");

	m_DLat = lat;
	m_DLng = lng;
	m_pMarker = marker;

	CAScheduler::schedule(schedule_selector(MapViewController::amapmarkerdragend), this, 0);
}

void MapViewController::onAmapMarkerClick(AmapMarker* marker)
{
	CCLog("void MapViewController::onAmapMarkerClick()--------------------------------------");

	m_pMarker = marker;

	CAScheduler::schedule(schedule_selector(MapViewController::amapmarkerclick), this, 0);
}

void MapViewController::onAmapMarkerInfoWindowClick(AmapMarker* marker)
{
	CCLog("void MapViewController::onAmapMarkerInfoWindowClick()--------------------------------------");

	m_pMarker = marker;

	CAScheduler::schedule(schedule_selector(MapViewController::amapmarkerinfowindowclick), this, 0);
}

void MapViewController::amaploaded(float t)
{
	CCLog("void MapViewController::amaploaded()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amaploaded), this);

	m_pLoadedLb->setText(UTF8("地图加载成功！！！"));
}

void MapViewController::amapclicked(float t)
{
	CCLog("void MapViewController::amapclicked()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amapclicked), this);

	string latlngstr = crossapp_format_string(UTF8("纬度：%f, 经度：%f"), m_DLat, m_DLng);
	m_pLatLngLb->setText(latlngstr);
}

void MapViewController::amapmarkerdragstart(float t)
{
	CCLog("void MapViewController::amapmarkerdragstart()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amapmarkerdragstart), this);

	string str = m_pMarker->getTitle() + UTF8("marker开始") + crossapp_format_string(UTF8(" 旧的纬度：%f, 旧的经度：%f \n新的纬度：%f, 新的经度：%f"), m_pMarker->getLat(), m_pMarker->getLng(), m_DLat, m_DLng);
	m_pLatLngLb->setText(str);
}

void MapViewController::amapmarkerdrag(float t)
{
	CCLog("void MapViewController::amapmarkerdrag()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amapmarkerdrag), this);

	string str = m_pMarker->getTitle() + UTF8("marker拖动") + crossapp_format_string(UTF8(" 旧的纬度：%f, 旧的经度：%f \n新的纬度：%f, 新的经度：%f"), m_pMarker->getLat(), m_pMarker->getLng(), m_DLat, m_DLng);
	m_pLatLngLb->setText(str);
}

void MapViewController::amapmarkerdragend(float t)
{
	CCLog("void MapViewController::amapmarkerdragend()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amapmarkerdragend), this);

	string str = m_pMarker->getTitle() + UTF8("marker拖动结束") + crossapp_format_string(UTF8(" 旧的纬度：%f, 旧的经度：%f \n新的纬度：%f, 新的经度：%f"), m_pMarker->getLat(), m_pMarker->getLng(), m_DLat, m_DLng);
	m_pLatLngLb->setText(str);
}

void MapViewController::amapmarkerclick(float t)
{
	CCLog("void MapViewController::amapmarkerclick()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amapmarkerclick), this);

	string str = m_pMarker->getTitle() + UTF8("marker点击") + crossapp_format_string(UTF8(" 纬度：%f, 经度：%f"), m_pMarker->getLat(), m_pMarker->getLng());
	m_pLatLngLb->setText(str);
}

void MapViewController::amapmarkerinfowindowclick(float t)
{
	CCLog("void MapViewController::amapmarkerinfowindowclick()--------------------------------------");

	CAScheduler::unschedule(schedule_selector(MapViewController::amapmarkerinfowindowclick), this);

	string str = m_pMarker->getTitle() + UTF8("marker窗口点击") + crossapp_format_string(UTF8(" 纬度：%f, 经度：%f"), m_pMarker->getLat(), m_pMarker->getLng());
	m_pLatLngLb->setText(str);
}

void MapViewController::onAmapLocationEnd(int code, double lat, double lng, const string& formatAddr, const string& province, const string& city, const string& district, const string& street, const string& number, const string& poi, const string& adcode)
{
	CCLog("void MapViewController::onAmapLocationEnd(int code, double lat, double lng) code:%d \nlat:%f \nlng:%f \nformatAddr:%s \nprovince:%s \ncity:%s \ndistrict:%s \nstreet:%s \nnumber:%s \npoi:%s \nadcode:%s", code, lat, lng, formatAddr.c_str(), province.c_str(), city.c_str(), district.c_str(), street.c_str(), number.c_str(), poi.c_str(), adcode.c_str());
}
